package com.sojson.project.controller.param.verify;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.sojson.enums.EActivityGetMethod;
import com.sojson.enums.core.EActivityScopeUser;
import com.sojson.project.entity.po.Activity;
import com.sojson.result.Result;
import com.sojson.util.DateUtil;
import com.sojson.util.StringUtil;

/**
 * 活动表参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class ActivityParamVerify {

    /**
     * 单条插入
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    public static Result<Integer> insert(Activity activity) {
        if (StringUtil.isBlank(activity.getActivityName())) {
            return Result.fail("活动标题不能为空！");
        }
        if (StringUtil.isBlank(activity.getThumb())) {
            return Result.fail("缩略图不能为空！");
        }
        if (StringUtil.isBlank(activity.getDes())) {
            return Result.fail("简单介绍不能为空！");
        }
        if (StringUtil.isBlank(activity.getPrizeType())) {
            return Result.fail("奖品类型不能为空！");
        }
        Integer getMethod = activity.getGetMethod();
        if (StringUtil.isBlank(getMethod)) {
            return Result.fail("领取方式不能为空！");
        }
        if (getMethod == EActivityGetMethod.HELP.getCode()) {
            Integer helpCount = activity.getHelpCount();
            if (StringUtil.isBlank(helpCount) || helpCount < 1) {
                return Result.fail("邀请助力次数不能为空！");
            }
        }
        if (StringUtil.isBlank(activity.getBindCouponNo())) {
            return Result.fail("优惠券编号不能为空！");
        }
        Integer joinCount = activity.getJoinCount();
        if (StringUtil.isBlank(joinCount) || joinCount < 1) {
            return Result.fail("参与次数限制不能为空！");
        }
        if (StringUtil.isBlank(activity.getStartTime())) {
            return Result.fail("活动开始时间不能为空！");
        }
        Date endTime = activity.getEndTime();
        if (StringUtil.isBlank(endTime)) {
            return Result.fail("活动结束时间不能为空！");
        }
        if (endTime.getTime() - DateUtil.nowLong() <= 0) {
            return Result.fail("活动结束时间必须大于当前时间！");
        }
        if (StringUtil.isBlank(activity.getSortNum())) {
            return Result.fail("排序不能为空！");
        }
        Integer scopeUser = activity.getScopeUser();
        if (StringUtil.isBlank(scopeUser)) {
            return Result.fail("参与用户范围不能为空！");
        }
        if (scopeUser == EActivityScopeUser.APPOINT.getCode()) {
            String useUserNo = activity.getUseUserNo();
            if (StringUtil.isBlank(useUserNo)) {
                return Result.fail("参与用户编号不能为空！");
            }
            activity.setUserNos(null);
        } else if (scopeUser == EActivityScopeUser.APPOINT_SOME_YES.getCode()
            || scopeUser == EActivityScopeUser.APPOINT_SOME_NO.getCode()) {
            List<String> userNos = activity.getUserNos();
            if (StringUtil.isBlank(userNos)) {
                return Result.fail("用户参与范围集合不能为空！");
            }
            activity.setUseUserNo(null);
        } else {
            activity.setUseUserNo(null);
            activity.setUserNos(null);
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    public static Result<?> updatePartialById(Activity activity) {
        if (StringUtil.isBlank(activity.getId())) {
            return Result.fail("ID不能为空！");
        }
        Integer helpCount = activity.getHelpCount();
        if (StringUtil.isNotBlankObject(helpCount) && helpCount < 1) {
            return Result.fail("邀请助力次数必须大于0！");
        }
        Integer joinCount = activity.getJoinCount();
        if (StringUtil.isNotBlankObject(joinCount) && joinCount < 1) {
            return Result.fail("参与次数限制必须大于0！");
        }

        return Result.success();
    }

    /**
     * 通过id修改状态
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    public static Result<?> updateStatus(Activity activity) {
        if (StringUtil.isBlank(activity.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(activity.getStatus())) {
            return Result.fail("状态不能为空！");
        }

        return Result.success();
    }

    /**
     * 活动助力
     * 
     * @param activity
     * @return
     * @throws Exception 
     */
    public static Result<?> help(String helpCode) {
        if (StringUtil.isBlank(helpCode)) {
            return Result.fail("助力代码不能为空！");
        }

        return Result.success();
    }

}